<template>
	<view>
		<button @click="chooseBankCard" type="primary">选择银行卡</button>
		<image :src="picture"></image>
		<!-- <view>--{{ bankCard }}</view> -->
		<button @click="submitBankCard">保存&下一步</button>
		<!-- <navigator url="/pages/id-card/id-card">
			<button>开发专用 下一步</button>
		</navigator> -->
		<view class="id-card-container">
			<uni-card title="银行卡信息">
				<!-- 用户名 -->
				<uni-forms-item label="银行">
					<uni-easyinput v-model="bankCard.bankName" :disabled="true" />
				</uni-forms-item>

				<!-- 身份证号 -->
				<uni-forms-item label="卡号">
					<uni-easyinput v-model="bankCard.bankCardNo" :disabled="true" />
				</uni-forms-item>
			</uni-card>
		</view>
	</view>
</template>

<script>
import { bankCardOcr } from '@/api/companyApi.js';
import { saveCurrentBankCard } from '@/api/bankCardApi.js';
export default {
	//   db.setBankCardNo(bankcard.getBankCardNo());
	// db.setBankName(bankcard.getBankName());
	data() {
		return {
			picture: '',
			bankCard: {
				id: 0,
				bankName: '',
				bankCardNo: '',
				comId: -1
			},
			readSuccess: false
		};
	},
	methods: {
		async chooseBankCard() {
			const res = await uni.chooseImage({
				count: 1
			});
			// console.log(res);
			this.picture = res.tempFilePaths[0];
			console.log(this.picture);
			const resp = await bankCardOcr(this.picture);
			console.log(resp);
			if (resp.error_code) {
				uni.showToast({
					title: '图片类型错误，重新上传'
				});
				return;
			}
			// bank_card_number: "6222 3588 8888 8888"
			// bank_card_type: 2
			// bank_name: "工商银行"
			// holder_name: "MU DAN KA"
			// valid_date: "88/88"
			if (!resp.result) {
				uni.showToast({
					title: '读取信息失败'
				});
				return;
			}
			uni.showToast({
				title: '银行卡读取成功'
			});
			this.bankCard.bankCardNo = resp.result.bank_card_number.replaceAll(' ', '');
			this.bankCard.bankName = resp.result.bank_name;
			this.readSuccess = true;
		},
		async submitBankCard() {
			if (!this.readSuccess) {
				uni.showToast({
					title: '请先上传银行卡'
				});
				return;
			}
			const res = await saveCurrentBankCard(this.bankCard);
			console.log(res);
			this.readSuccess = false;
			uni.navigateTo({
				url: '/pages/id-card/id-card'
			});
		}
	}
};
</script>

<style>
.id-card-container {
	padding: 20rpx;
}
</style>
